(function($){
	
    $(function(){
    	
		/**
		 * 奖品入库
		 */
		//G币奖品入库,计算G币，根据(活动max-奖品分配的)
        $('#storageGold').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            buttons: {
                '确定': function(){
                	var awardId = $('#awID').val();
                	var activeId  =$('#activeId').val();
                	var flag = $("#gbStoreForm").validate(VALIDATE.gbStore).form();
                	if(flag){
                		var gbnum = parseInt($('#gbnum').val());
                    	var gbAmount =parseInt($('#gbAmount').val());
                    	var availableGb  = parseInt($('#availableGb').text());
                    	if(gbnum*gbAmount>availableGb){//超过可用gb
                    		showTips("输入数值超过可用G币数");
                    	}else{//ajax方法
                    		$.ajax({
                    			type : "POST",
                    			url : ctx+"/award/ajaxStoreGb.do",
                    			data : "awardId="+awardId+"&gbnum="+gbnum+"&gbAmount="+gbAmount+"&activeId="+activeId,
                    			success : function(msg){
                    				if(msg.startWith("error:")){
                    					showTips("出错了:"+msg.substring(6,msg.length));
                    				}else{
                    					showTips("执行成功",function(){
              								$(this).dialog('close');
              								window.location.reload();
              							},function(){
              								$(this).dialog('close');
              								window.location.reload();
              							});
                    				}
                    			}
                    			
                    		});
                    	}
                    	
                	}
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
		
		//经验值入库
        $('#storageExperience').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            modal: true,
            buttons: {
                '确定': function(){
                	var awardId = $('#awID').val();
                	var activeId  =$('#activeId').val();
                	var flag = $("#experienceStoreForm").validate(VALIDATE.experienceStore).form();
                	var experienceNum = $('#experienceNum').val();
                	var experienceAmount = $('#experienceAmount').val();
                	if(flag){
                		$.ajax({
                			type : "POST",
                			url : ctx+"/award/ajaxStoreExperience.do",
                			data : "awardId="+awardId+"&experienceNum="+experienceNum+"&experienceAmount="+experienceAmount+"&activeId="+activeId,
                			success : function(msg){
                				if(msg.startWith("error:")){
                					showTips("出错了!请确定总入库值小于等于2147483647。");
                				}else{
                					showTips("执行成功",function(){
          								$(this).dialog('close');
          								window.location.reload();
          							},
          							function(){
          								$(this).dialog('close');
          								window.location.reload();
          							});
                				}
                			}
                		});
                		
                	}
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
	        
		//实物奖品入库
        $('#storageNoneVirtual').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            modal: true,
            buttons: {
                '确定': function(){
                	var awardId = $('#awID').val();
                	var activeId  =$('#activeId').val();
                	var flag = $("#entityStoreForm").validate(VALIDATE.entityStore).form();
                	var awardNum = $('#entitynum').val();
                	if(flag){//验证通过，ajax
                		$.ajax({
                			type : "POST",
                			url :  ctx+"/award/ajaxStoreEntity.do",
                			data: "awardId="+awardId+"&awardNum="+awardNum+"&activeId="+activeId,
                			success: function(msg){
      						  if(msg.startWith("error:")){
      							  showTips("出错了:"+msg.substring(6,msg.length));
      						  }else{
      							showTips("执行成功",function(){
      								$(this).dialog('close');
      								window.location.reload();
      							},
      							function(){
      								$(this).dialog('close');
      								window.location.reload();
      							});
      						  }
      					  }
                		});
                	}
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
	        
		//奖品入库链接
		$('.storage_link').click(function(){
			var type = $(this).attr('awardType');
			var awardId = $(this).attr('awardId');
			$('#awID').attr('value',awardId);
			switch (type) {
			case '1':
				//实物
				$('#storageNoneVirtual').dialog('open');
				break;
			case '2':
				//虚拟物品
				$('#storageVirtual').dialog('open');
				break;
			case '3':
				//G币
				//ajax填充一下
				$.ajax({
					type:"POST",
					url:ctx+"/award/ajaxGetSurplusGb.do",
					data: "awardId="+awardId,
					success: function(msg){
						  if(msg.startWith("error:")){
							  showTips("出错了:"+msg.substring(33,msg.length));
						  }else{
							  $('#availableGb').text(msg);
							  $('#storageGold').dialog('open');
							 
						  }
					  }
					
				});
				
				break;
			default:
				//经验值
				$('#storageExperience').dialog('open');
			}
		});
		
		//虚拟点卡奖品入库
        $('#storageVirtual').dialog({
            autoOpen: false,
            height: 300,
            width: 560,
            modal: true,
            buttons: {
                '确定': function(){
                	var txtFile = $('#virtualTxt');
                	var awardId = $('#awID').val();
                	var activeId  =$('#activeId').val();
                	if(txtFile!==''&&txtFile!==undefined){
                		ajaxUpload(txtFile,awardId,activeId);
                	}
                	else{
                		showTips("请选择文件");
                	}
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
		
		//追加奖品(填入数字的约束)
        $('#addMoreAward').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            modal: true,
            buttons: {
                '确定': function(){
                	$(this).trigger("ok");
                },
                '取消': function(){
                    $(this).dialog('close');
                }
            }
        });
		
        //追加奖品链接点击
		$('.add-more-award-link').click(function(e){
            e.preventDefault();
            var type = $(this).attr('awardType');
			var awardId = $(this).attr('awardId');
			var gnum = $(this).attr('gnum');
			$('#awID').attr('value',awardId);
			if(type ==='3'){
				$('#gnum').text(gnum+'G币每份');
			}else if(type ==='4'){
				$('#gnum').text(gnum+'经验每份');
			}else{
				$('#gnum').text('');
			}
			
			if(type ==='2'){	//虚拟
				$('#storageVirtual').dialog('open');
			}else{
				$('#addMoreAward').dialog('open').bind("ok", function() {
					var awardId = $('#awID').val();
                	var awardNum = $('#awardNum').val();
                	var activeId = $('#activeId').val();
                	
                	if(awardNum!==''&&awardNum!==undefined&&awardNum!=null){
                		
                		if (type === '1') {
                			//实物
                			if(parseInt(awardNum)>100000) {
                				$('#addMoreAward').find('.validate-tips').text("请输入小于10万的数字");
                				return;
                			}
            			} else {
            				//经验值、G币
            				if(parseInt(awardNum)>1000000) {
                				$('#addMoreAward').find('.validate-tips').text("请输入小于100万的数字");
                				return;
                			}
            			}
            			//ajax 提交
                		$.ajax({
                			type : "POST",
                			url : ctx+"/award/ajaxAddToAward.do",
                			data : "awardId="+awardId+"&awardNum="+awardNum+"&activeId="+activeId,
                			success : function(msg){
                				if(msg.startWith("error:")){
                					showTips("出错了:"+msg.substring(6,msg.length));
                				}else{
                					showTips("执行成功",function(){ window.location.reload();},
                							function(){
                						window.location.reload();
                					});
                				}
                			}
                		
                		});
                		
                	}
                	else{
                		$('#addMoreAward').find('.validate-tips').text("请输入数字");
                	}
				});
			}
           
        });
		
		
		//删除奖品
		$('.is-delete').click(function(e){
			e.preventDefault();
			var awardId = $(this).parent().parent().parent().find('td').first().text();
			var activeId = $(this).attr('activeId');
			$.ajax({
				  type: "POST",
				  url: ctx+"/award/removeAward.do",
				  data: "awardId="+awardId+"&activeId="+activeId,
				  success: function(msg){
					  if(msg.startWith("error:")){
						  showTips("出错了:"+msg.substring(6,msg.length));
					  }else{
						   showTips("执行成功",function(){ window.location.reload();},
								   function(){
							   window.location.reload();
						   });
					  }
				  }
				});
		});
        
    });
    /*
     * ajax上传文件
     * 
     */
    function ajaxUpload(txtFile,awardId,activeId) {
		if(txtFile.val()==""){
			showTips("请选择txt文件");
			return false;
		}
		var txtid = txtFile.attr('id');
		$.ajaxFileUpload ({
			url :ctx+'/award/ajaxUploadTXT.do?awardId='+awardId+'&activeId='+activeId,
			secureuri :false,
			fileElementId :txtid,
			dataType : 'text',
			success : function (data, status){
				if(data.startWith("error")){
					showTips(data);
				}else{
					showTips("上传成功",function(){ window.location.reload();},
							function(){
						window.location.reload();
					});
				}
			},
			error: function (data, status, e){
				showTips(status);
			}
		})
		return false;
	}// end ajaxUpload
   

})(jQuery);